package tt.love.hw.action;


import java.util.Date;
import java.util.List;


import javax.jdo.PersistenceManager;


import tt.love.hw.EmailTemplateFactory;
import tt.love.hw.PMF;
import tt.love.hw.common.ActionContext;
import tt.love.hw.common.ViewRender;
import tt.love.hw.email.EmailTemplate;
import tt.love.hw.jdo.Application;
import tt.love.hw.jdo.User;
import tt.love.hw.service.ApplicationService;
import tt.love.hw.service.EmailService;

public class ApplicationAction {
	private ApplicationService applicationService;
	private EmailService emailService;
	
	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}
	
	
	public void setEmailService(EmailService emailService) {
		this.emailService = emailService;
	}


	public ViewRender request(ActionContext actionContext){	
		Application app = (Application)actionContext.get("application");
		app.setAppDate(new Date());
		app.setRequestUserId(actionContext.getUserSessionInfo().getUserid());
		app.setStatus("Pending");

		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(app);
			User mateUser = pm
					.getObjectById(User.class,actionContext.getUserSessionInfo().getMateuserid());
			emailService.send(
					mateUser,
					EmailTemplateFactory.get()
							.createEmailTemplate(
									EmailTemplate.EMAIL_TEMPLATE_APP_REQUEST,
									new String[][] { { "name",
											mateUser.getUserName() } }));
		} finally {
			pm.close();
		}
		return new ViewRender("/process/getMyPendingApplication.tt");
	}
	public ViewRender getMyPendingApplication(ActionContext actionContext){
		List<Application> appList = applicationService.getPendingApplication(actionContext.getUserSessionInfo().getUserid());
		actionContext.put("appList", appList);
		actionContext.put("type", "delete");
		return new ViewRender("view.application.list");
	}
	public ViewRender getMyCompleteApplication(ActionContext actionContext){
		List<Application> appList = applicationService.getApproveApplication(actionContext.getUserSessionInfo().getUserid());
		appList.addAll(applicationService.getRejectApplication(actionContext.getUserSessionInfo().getUserid()));
		appList.addAll(applicationService.getCancelApplication(actionContext.getUserSessionInfo().getUserid()));
		actionContext.put("appList", appList);
		actionContext.put("type", "cancelMy");
		return new ViewRender("view.application.list");
	}
	public ViewRender getHisPendingApplication(ActionContext actionContext){
		List<Application> appList = applicationService.getPendingApplication(actionContext.getUserSessionInfo().getMateuserid());
		actionContext.put("appList", appList);
		actionContext.put("type", "assess");
		return new ViewRender("view.application.list");
	}
	public ViewRender getHisCompleteApplication(ActionContext actionContext){
		List<Application> appList = applicationService.getApproveApplication(actionContext.getUserSessionInfo().getMateuserid());
		appList.addAll(applicationService.getRejectApplication(actionContext.getUserSessionInfo().getMateuserid()));
		appList.addAll(applicationService.getCancelApplication(actionContext.getUserSessionInfo().getMateuserid()));
		actionContext.put("appList", appList);
		actionContext.put("type", "cancelHis");
		return new ViewRender("view.application.list");
	}
	public ViewRender rejectHisApp(ActionContext actionContext){
		updateAppStatus(actionContext,"Reject");
		sendEmail(actionContext,EmailTemplate.EMAIL_TEMPLATE_APP_REJECT);
		return new ViewRender("/process/getHisPendingApplication.tt");
	}
	public ViewRender deleteMyPendingApp(ActionContext actionContext){
		String appId = (String)actionContext.get("appId");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Application app = pm.getObjectById(Application.class, Long
					.valueOf(appId));
			pm.deletePersistent(app);
		} finally {
			pm.close();
		}
		return new ViewRender("/process/getMyPendingApplication.tt");
	}
	public ViewRender cancelHisApproveApp(ActionContext actionContext){
		updateAppStatus(actionContext,"Cancel");
		sendEmail(actionContext,EmailTemplate.EMAIL_TEMPLATE_APP_CANCEL);
		return new ViewRender("/process/getHisCompleteApplication.tt");
	}
	public ViewRender cancelMyApproveApp(ActionContext actionContext){
		updateAppStatus(actionContext,"Cancel");
		sendEmail(actionContext,EmailTemplate.EMAIL_TEMPLATE_APP_CANCEL_OWN);
		return new ViewRender("/process/getMyCompleteApplication.tt");
	}
	public ViewRender approveHisApp(ActionContext actionContext){
		updateAppStatus(actionContext,"Approve");
		sendEmail(actionContext,EmailTemplate.EMAIL_TEMPLATE_APP_APPROVE);
		return new ViewRender("/process/getHisPendingApplication.tt");
	}
	public void updateAppStatus(ActionContext actionContext,String status){
		String appId = (String)actionContext.get("appId");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Application app = pm.getObjectById(Application.class, Long
					.valueOf(appId));
			app.setStatus(status);	
		} finally {
			pm.close();
		}
	}
	
	public void sendEmail(ActionContext actionContext, int emailTemplate){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			User mateUser = pm.getObjectById(User.class, actionContext.getUserSessionInfo()
					.getMateuserid());
			emailService.send(
					mateUser,
					EmailTemplateFactory.get()
							.createEmailTemplate(emailTemplate,
									new String[][] { { "name",
											mateUser.getUserName() } }));
		
		} finally {
			pm.close();
		}
	}
	
}
